﻿using AutoMapper;
using B.S.Sterilizationcenter.Domain.zyy;
using B.S.Sterilizationcenter.ErrorCode;
using B.S.Sterilizationcenter.Writer.API.Appliaction.Commends.Zyy.Usages;
using MediatR;
using SqlSugar;

namespace B.S.Sterilizationcenter.Writer.API.Appliaction.CommandHandlers.Zyy.Usages
{
    public class DeleteUsageHandler : IRequestHandler<DeleteUsageCommand, ApiResult<int>>
    {
        private readonly ISqlSugarClient db;
        private readonly IMapper mapper;

        public DeleteUsageHandler(ISqlSugarClient db,IMapper mapper)
        {
            this.db = db;
            this.mapper = mapper;
        }

        public  async Task<ApiResult<int>> Handle(DeleteUsageCommand request, CancellationToken cancellationToken)
        {

            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var del = await db.Queryable<UsageManagement>().FirstAsync(p => p.UsageId == request.UsageId);
                del.IsDelete = true;
                var res = await db.Updateable<UsageManagement>(del).ExecuteCommandAsync();
                if (res > 0)
                {
                    result.Code = ApiEnums.删除成功;
                    result.Msg = "删除成功";
                }

                else
                {
                    result.Code = ApiEnums.删除失败;
                    result.Msg = "删除失败";
                }
                return result;
            }
            catch (Exception)
            {

                throw;
            }
            
          
        }
    }
}
